package com.jrx.anytxn.transaction.job.collection.step;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.TlCollectionLoanList;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlCollectionLoanListMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CollectionProcessor implements ItemProcessor<AmLoanAccountInfo, TlCollectionLoanList> {

    Logger logger = LoggerFactory.getLogger(CollectionProcessor.class);
    @Resource
    private ExtTlCollectionLoanListMapper extTlCollectionLoanListMapper;
    @Autowired
    private ISegmentService segmentService;

    @Override
    public TlCollectionLoanList process(AmLoanAccountInfo amLoanAccountInfo) throws Exception {
        String loanId = amLoanAccountInfo.getLoanId();
        TlCollectionLoanList tlCollectionLoanList = extTlCollectionLoanListMapper.selectByLoanId(loanId);
        if(tlCollectionLoanList != null){
            logger.info("订单已经入催，无需处理,订单号：{}",loanId);
            return null;
        }
        TlCollectionLoanList info = BeanMapping.copy(amLoanAccountInfo,TlCollectionLoanList.class);
        try {
            info.setId(segmentService.getId(TransBizConstant.TL_PATMENT_LOG_ID));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(TxnRespCode.ERROR.getUserTips());
        }
        return info;
    }
}
